package com.wzdigit.wms.raw.controller.outstock;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.outstock.CancelPickMaterialRequest;
import com.wzdigit.wms.basic.client.request.outstock.PickMaterialRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.outstock.*;
import com.wzdigit.wms.basic.enums.GdnTypeEnum;
import com.wzdigit.wms.basic.enums.outstock.RawPickEnum;
import com.wzdigit.wms.raw.constant.RawWareConstant;
import com.wzdigit.wms.raw.service.gdn.DeliveryNewService;
import com.wzdigit.wms.raw.service.outstock.PickMaterialService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 拣料
 * 
 * @author Administrator
 *
 */
@RestController
@Api(tags = "拣料")
@RequestMapping(value = RawWareConstant.API_VERSION)
public class PickMaterialController {

	public static final String REQUEST_MODEL = "/PickMaterial";

	@Autowired
	private PickMaterialService pickMaterialService;

	@Autowired
	private DeliveryNewService deliveryNewService;


	@ApiOperation(value = "下拉框修改仓库")
	@GetMapping(value = REQUEST_MODEL + "/gdn/whs")
	public void updateWhs(
			Integer gdnHeaderId,Integer whsId,
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
		pickMaterialService.setGdnWhs(gdnHeaderId, whsId, servletRequest);
	}

	@ApiOperation(value = "拣料通知页面")
	@GetMapping(value = REQUEST_MODEL + "/search")
	public PagerInfo<List<PickNoticeHeaderResponse>> search(
			PickMaterialRequest dto, Pager<?> pager,
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
		try {
			return pickMaterialService.search(dto, pager, servletRequest, servletResponse);
		} catch (RestStatusException e) {
			e.printStackTrace();
			servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			throw e;
		}
	}

	@ApiOperation(value = "拣料单据类型")
	@GetMapping(value = REQUEST_MODEL + "/getPickMaterialList")
	public Result<List<GdnTypeEnum>> getPickMaterialList(
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
		try {
            return pickMaterialService.getPickMaterialList();
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}

	@ApiOperation(value = "拣料单据状态")
	@GetMapping(value = REQUEST_MODEL + "/getPickMaterialStatusList")
	public Result<List<RawPickEnum>> getPickMaterialStatusList(
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
		try {
            return pickMaterialService.getPickMaterialStatusList();
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}

	@ApiOperation(value = "拣料,查询该单头下的物料集合")
	@GetMapping(value = REQUEST_MODEL + "/pick/search/sku/{gdnHeaderId}/{isOwnSelect}")
	public Result<List<PickMaterialSkuResponse>> getSkuByGdnHeaderId(
			@ApiParam("发货单单头ID") @PathVariable("gdnHeaderId") Long gdnHeaderId,
			@ApiParam("发货单单头ID") @PathVariable("isOwnSelect") Integer isOwnSelect, 
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
		try {
            return pickMaterialService.getSkuByGdnHeaderId(gdnHeaderId, isOwnSelect, servletRequest, servletResponse);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}

	@ApiOperation(value = "拣料页面查询信息")
	@GetMapping(value = REQUEST_MODEL + "/pick/search/page/{gdnHeaderId}/{skuId}")
	public Result<PickInfoResponse> getPickInfo(
			@ApiParam("发货单单头ID") @PathVariable("gdnHeaderId") Integer gdnHeaderId,
			@ApiParam("物料ID") @PathVariable("skuId") Integer skuId, 
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
		try {
            return pickMaterialService.getPickInfo(gdnHeaderId, skuId, servletRequest, servletResponse);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}

	@ApiOperation(value = "推荐库位集合查询")
	@GetMapping(value = REQUEST_MODEL + "/pick/loc/list/{whsId}/{skuId}/{gdnHeaderId}")
	public Result<List<LocResponse>> getLoc(
			@ApiParam("发料仓ID") @PathVariable("whsId") Integer whsId,
			@ApiParam("物料ID") @PathVariable("skuId") Integer skuId,
			@ApiParam("发货单单头ID") @PathVariable("gdnHeaderId") Integer gdnHeaderId, 
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
		try {
            return pickMaterialService.getLoc(whsId, skuId, gdnHeaderId, servletRequest, servletResponse);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}

	@ApiOperation(value = "扫描获取推荐库位")
	@GetMapping(value = REQUEST_MODEL + "/pick/loc/scan/{whsId}/{skuId}/{gdnHeaderId}/{locCode}")
	public Result<LocResponse> getLocByLocCode(
			@ApiParam("发料仓ID") @PathVariable("whsId") Integer whsId,
			@ApiParam("物料ID") @PathVariable("skuId") Integer skuId,
			@ApiParam("发货单单头ID") @PathVariable("gdnHeaderId") Integer gdnHeaderId,
			@ApiParam("库位代码") @PathVariable("locCode") String locCode, 
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
		try {
            return pickMaterialService.getLocByCode(whsId, skuId, gdnHeaderId, locCode, servletRequest, servletResponse);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}

	@ApiOperation(value = "拣料明细")
	@GetMapping(value = REQUEST_MODEL + "/getPickDtlInfo")
	public Result<List<PickSkuResponse>> getPickDtlInfo(
			@ApiParam("发货单单头ID") Integer gdnHeaderId,
			@ApiParam("物料CODE") String skuCode, 
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
		try {
            return pickMaterialService.getPickDtlInfo(gdnHeaderId, skuCode, servletRequest, servletResponse);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}

	@ApiOperation(value = "SN明细")
	@GetMapping(value = REQUEST_MODEL + "/getPickDtlInfo/{skuId}/{gdnHeaderId}")
	public Result<List<PickSnDtlResponse>> getSnInfo(
			@ApiParam("物料ID") @PathVariable("skuId") Integer skuId,
			@ApiParam("发货单单头ID") @PathVariable("gdnHeaderId") Integer gdnHeaderId, 
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
		try {
            return pickMaterialService.getSnInfo(skuId, gdnHeaderId, servletRequest, servletResponse);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}

	@ApiOperation(value = "拣料扫描SN")
	@PostMapping(value = REQUEST_MODEL + "/pick/scan/{gdnHeaderId}/{skuId}/{sn}/{whsId}")
	public Result<ScanResponse> scanSn(
			@ApiParam("发货单单头ID") @PathVariable("gdnHeaderId") Integer gdnHeaderId,
			@ApiParam("物料ID") @PathVariable("skuId") Integer skuId, 
			@ApiParam("条码") @PathVariable("sn") String sn,
			@ApiParam("发料仓ID") @PathVariable("whsId") Integer whsId, 
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
		try {
            return pickMaterialService.scanSn(gdnHeaderId, skuId, sn, whsId, servletRequest, servletResponse);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}




	@ApiOperation(value = "取消拣料")
	@PostMapping(value = REQUEST_MODEL + "/cancel/pick")
	public Result<List<ScanResponse>> cancelPick(
			@RequestBody List<CancelPickMaterialRequest> dto,
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
		try {
            return pickMaterialService.cancelPick(dto, servletRequest, servletResponse,true,null);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}

	@ApiOperation(value = "拣料提交按钮权限控制", notes = "拣料提交按钮权限控制")
	@ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
	@RequestMapping(value = REQUEST_MODEL + "/commit/permission", method = RequestMethod.GET)
	public Result<?> checkPermission(
			BigDecimal gdnHeaderId,
			 HttpServletResponse servletResponse) throws Exception {
		try {
			return pickMaterialService.checkPermission(gdnHeaderId);
		}
		catch (RestStatusException e) {
			e.printStackTrace();
			servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			throw e;
		}
	}

	//拣料提交 代替交接确认
	@ApiOperation(value = "拣料提交 代替交接确认", notes = "拣料提交 代替交接确认")
	@ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
	@RequestMapping(value = REQUEST_MODEL + "/commit", method = RequestMethod.GET)
	public Result<?> deliveryCommitNew(
			BigDecimal gdnHeaderId,
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
		try {
			return deliveryNewService.deliveryCommit(gdnHeaderId,servletRequest,servletResponse);
		}
		catch (RestStatusException e) {
			e.printStackTrace();
			servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			throw e;
		}
	}


	@ApiOperation(value = "拆分物料")
	@PostMapping(value = REQUEST_MODEL + "/split/{gdnHeaderId}/{skuId}/{sn}/{whsId}/{splitcount}")
	public Result<ScanResponse> split(
			@ApiParam("发货单单头ID") @PathVariable("gdnHeaderId") Integer gdnHeaderId,
			@ApiParam("物料ID") @PathVariable("skuId") Integer skuId, 
			@ApiParam("条码") @PathVariable("sn") String sn,
			@ApiParam("发料仓ID") @PathVariable("whsId") Integer whsId,
			@ApiParam("拆分数量") @PathVariable("splitcount") Integer splitcount, 
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
		try {
            return pickMaterialService.split(gdnHeaderId, skuId, sn, whsId, splitcount, servletRequest, servletResponse);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}
}
